<?php
class Administrator_AmController extends Zend_Controller_Action
{
	public function init()
	{
		$member=new CMember();
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){	
			$this->_redirect(MY_URL.'/administrator-auth-login');
		}
		
	}
	
	public function preDispatch()
	{
		$this->getHelper ('layout')->setLayout('framelayout');
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
	}
	
	public function indexAction()
	{
		
	}
	
	public function cashAction(){
		$cashModel = new CAmcashlog();
		$memberModel = new CMember();
		$page = $this->getRequest()->getParam('page', 1);
		$pageNum = 5;
		
		$type = $this->getRequest()->getParam('type', 5);
		$uid = $_COOKIE[COOKIE_PRE.'uid'];
		$cashList = $cashModel->getAll($page, $pageNum, $type);

		$pageCount = $cashModel->getPageCount($pageNum, $type);
		$statistics = new CStatistics();
		foreach($cashList as $key=>$v){
			$field = array('uid'=>$v->uid);
			$username[] = $memberModel->getUsername($field);
			
		}
		
        
        $param = 'administrator-am-cash?type=' . $type . '&';
        $pageLink = $statistics->orderPage($pageCount, $pageNum, $page, $param);
        //var_dump($pageLink);die;
        $this->view->pageLink = $pageLink;
        $this->view->cashList = $cashList;
        
        $amModel = new CAm();
        
        $this->view->inProcess = $amModel->getInProcessCash();
        $this->view->success = $amModel->getSuccessCash();
        $this->view->refuse = $amModel->getRefuseCash();
        
        $this->view->type = $type;
        $this->view->username = $username;
        
	}
	
	public function casheditAction()
	{
		$cashModel = new CAmcashlog();
		$id = $this->getRequest()->getParam('id', 1);
		$cash = $cashModel->find($id)->current();
		
		$this->view->id = $id;
		$this->view->cash = $cash;
		
		if ($this->getRequest()->isPost()) {
			$type = $this->getRequest()->getParam('type', 0);
			$this->getHelper('layout')->disableLayout();
    		$this->getHelper('viewRenderer')->setNoRender();
			$amModel = new CAm();
			$amModel->doCash($id, $type);
			$this->view->showMsg('success', 'administrator-am-cash');	
		}		
	}
	
	public function viewAction()
	{
		$mlmMember = new CMlmmember();
		$rows = $mlmMember->fetchAll();
		$result = array();
		$mlm = new CMlm();
		foreach ($rows as $v) {
			$tmpCount = $mlm->getLevel1Uid($v->uid);
			
			if (!$tmpCount) {
				continue;
			}
			$tmp[0] = $tmpCount;
			$tmp[1] = $mlm->getLevel2Uid($v->uid);
			$tmp[2] = $mlm->getLevel3Uid($v->uid);
			$result[$v->uid] = $tmp;
		}
		$this->view->count = count($result);
		$this->view->result = $result;
	}
	public function userAction(){
		$page = $this->getRequest()->getParam('page', 1);
		$pageNum = 10;
		$ammemberModel = new CAmmember();
		$memberModel = new CMember();
		$users = $ammemberModel->getUsers($page,$pageNum);
		foreach($users as $key=>$v){
			$field = array('uid'=>$v['uid']);
			$info = $memberModel->getUsername($field);
			$users[$key]['email']=$info[0]['username'];
		}
		$pageCount = $ammemberModel->getUserCount();
		$statistics = new CStatistics();
		
		$param = 'administrator-am-user?';
		$pageLink = $statistics->orderPage($pageCount, $pageNum, $page, $param);
		//var_dump($pageLink);die;
		$this->view->pageLink = $pageLink;
		$this->view->users = $users;
	}
}